from django import forms
from django.core import validators
from teacher_front.models import StudentsAnswer

class Base(forms.Form):
    def get_error(self):
        news_errors = []
        errors = self.errors.get_json_data()
        for messages in errors.values():
            for message_dict in messages:
                for key,message in message_dict.items():
                    if key == 'message':
                        news_errors.append(message)
        return news_errors

class Students_login(Base):
    number = forms.CharField(
        max_length=12,
        validators=[validators.RegexValidator(r'\d{12}',message='请输入正确格式的学号！')],
        error_messages={
            'required':'请输入学号',
        }
    )

    password = forms.CharField(
        max_length=16,
        validators=[validators.MinLengthValidator(6)],
        error_messages={
            'required':'请输入密码',
            'min_length':'密码长度不低于6位',
            'max_length':'密码长度不超过16位'
        }
    )

class Changepass(Base):
    password = forms.CharField(
        max_length=16,
        validators=[validators.MinLengthValidator(6)],
        error_messages={
            'required': '请输入密码',
            'min_length': '密码长度不低于6位',
            'max_length': '密码长度不超过16位'
        }
    )

    new_password = forms.CharField(
        max_length=16,
        validators=[validators.MinLengthValidator(6)],
        error_messages={
            'required': '请输入密码',
            'min_length': '密码长度不低于6位',
            'max_length': '密码长度不超过16位'
        }
    )

    new_password_repeat = forms.CharField(
        max_length=16,
        validators=[validators.MinLengthValidator(6)],
        error_messages={
            'required': '请输入密码',
            'min_length': '密码长度不低于6位',
            'max_length': '密码长度不超过16位'
        }
    )

    def clean(self):
        clean_data = super().clean()
        new_password = clean_data.get('new_password')
        new_password_repeat = clean_data.get('new_password_repeat')
        if new_password != new_password_repeat:
            raise forms.ValidationError(message='两次输入的密码不一致!')
        else:
            return clean_data

class InputAnswer(forms.ModelForm):
    class Meta:
        model = StudentsAnswer
        fields = ['ans']